package com.fitns.user.controller.user;

import com.alibaba.fastjson.JSON;
import com.fitns.user.conf.ExcelConstants;
import com.fitns.user.conf.Resp;
import com.fitns.user.conf.ZhhwResp;
import com.fitns.user.dto.user.PasswordVO;
import com.fitns.user.dto.user.PrivilegeVO;
import com.fitns.user.dto.user.UserCondition;
import com.fitns.user.dto.user.UserVO;
import com.fitns.user.service.user.UserService;
import com.fitns.user.utils.ExcelUtils;
import com.fitns.user.utils.ResultData;
import com.github.pagehelper.PageInfo;
import com.xiaoleilu.hutool.util.CollectionUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.Synchronized;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/user")
@Api(description = "账户管理")
public class UserController {

    @Autowired
    private UserService userService;

    @PostMapping
    @ApiOperation("新增账户")
    public ResultData insert(@RequestBody UserVO userVO){
        UserVO userVO1=userService.insert(userVO);
        return new ResultData(200,"SUCESS",userVO1,"");
    }

    @GetMapping("/search")
    @ApiOperation("查询")
    public Object search(UserCondition userCondition){
        PageInfo<UserVO> userVOPageInfo=userService.search(userCondition);
        userVOPageInfo.setPageSize(userVOPageInfo.getPageSize());
        return   Resp.page(userVOPageInfo);
    }

    @PostMapping(value = "/importExcel", produces = {"text/html;charset=UTF-8"})
    @Synchronized
    @ApiOperation("导入账户")
    public String importExcel(@RequestParam("file") MultipartFile file) throws Exception{
        List<List<String>> excelList = ExcelUtils.importExcel(file);
        List<String> headList=excelList.get(0);
        List<UserVO> userVOList=new ArrayList<>();
        if(ExcelConstants.USER_EXCEL_HEADER.XUHAO.equals(headList.get(0))
                &&ExcelConstants.USER_EXCEL_HEADER.USERNAME.equals(headList.get(1))
                &&ExcelConstants.USER_EXCEL_HEADER.CONTRACT_NAME.equals(headList.get(2))
                &&ExcelConstants.USER_EXCEL_HEADER.TRUE_NAME.equals(headList.get(3))
                &&ExcelConstants.USER_EXCEL_HEADER.ROLE_NAME.equals(headList.get(4))
                &&ExcelConstants.USER_EXCEL_HEADER.ORGAN_NAME.equals(headList.get(5))
                &&ExcelConstants.USER_EXCEL_HEADER.LOVE_NAME.equals(headList.get(6))
                &&ExcelConstants.USER_EXCEL_HEADER.EMAIL.equals(headList.get(7))){
            userVOList= userService.importExcel(excelList);
        }else {
            UserVO userVO=new UserVO();
            userVO.setMsg("模板有误，请勿修改模板！");
            userVOList.add(userVO);
        }
        ZhhwResp zhhwResp=new ZhhwResp(null);
        String errorData="";
        if (CollectionUtil.isNotEmpty(userVOList)) {
            errorData = JSON.toJSONString(userVOList);
        }
        if(StringUtils.isEmpty(errorData)){
            zhhwResp.setMessage("成功");
            zhhwResp.setCode(200);
        }else{
            zhhwResp.setCode(300);
            zhhwResp.setMessage("失败");
        }

        zhhwResp.setData(errorData);
        return JSON.toJSONString(zhhwResp);
    }

    @PostMapping("/modify")
    @ApiOperation("修改")
    public ResultData modify(@RequestBody UserVO userVO){
        UserVO userVO1=userService.modify(userVO);
        return new ResultData(200,"SUCESS",userVO1,"");
    }

    @DeleteMapping
    @ApiOperation("删除")
    public ResultData delete(@RequestParam Long id){
        userService.delete(id);
        return new ResultData(200,"SUCESS",null,"");
    }

    @PostMapping("/updatePassword")
    @ApiOperation("修改密码")
    public ResultData updatePassword(@RequestBody PasswordVO passwordVO){
        userService.restPassword(passwordVO);
        return new ResultData(200,"SUCESS",null,"");
    }

    @GetMapping("/privilegeTree")
    @ApiOperation("获取用户权限树")
    public ResultData privilegeTree(){
        List<PrivilegeVO> privilegeVOS=  userService.getPrivilegeTree();
        return new ResultData(200,"SUCESS",privilegeVOS,"");

    }

    @GetMapping("/findCurrentUser")
    @ApiOperation("获取当前登录用户")
    public ResultData findCurrentUser(){
        UserVO userVO=userService.currentUser();
        return new ResultData(200,"SUCESS",userVO,"");
    }

    @PostMapping("/modifyCurrentUser")
    @ApiOperation("修改当前登录用户")
    public ResultData modifyCurrentUser(@RequestBody UserVO userVO){
        UserVO userVO1= userService.modifyCurrentUser(userVO);
        return new ResultData(200,"SUCESS",userVO1,"");

    }

    @PostMapping("/resetPassword")
    @ApiOperation("重置密码")
    public ResultData updatePassword(@RequestParam Long id){
        userService.resetPassword(id);
        return new ResultData(200,"SUCESS",null,"");
    }

}
